//
//  AppDelegate.m
//  WorldOfFacts_V2
//
//  Created by Denis Demjanko on 08.11.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "AppDelegate.h"
#import "ListOfFacts_VC.h"
#import "SCNavigationBar.h"
#import "SideMenuViewController.h"
#import "DetailFact_VC.h"

@implementation AppDelegate

@synthesize rootViewController,window;
@synthesize pushManager;

#pragma mark - init

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
    if(![[NSUserDefaults standardUserDefaults] valueForKey:@"isSave"]){
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"isSave"];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.rootViewController = [self sideMenu].navigationController;
    [self.window makeKeyAndVisible];
    
    //push
    pushManager = [[PushNotificationManager alloc] initWithApplicationCode:@"4ef2044dc63180.15585334" appName:@"Мир фактов" ];
	pushManager.delegate = self;
	[pushManager handlePushReceived:launchOptions];
    
    return YES;
}

#pragma mark - MFSideMenu

- (MFSideMenu *)sideMenu {
    SideMenuViewController *sideMenuController = [[SideMenuViewController alloc] init];
    UINavigationController *navigationController = [self customizedNavigationController:[[ListOfFacts_VC alloc] initWithNibName:@"ListOfFacts_VC" bundle:nil]];
    
    MFSideMenuOptions options = MFSideMenuOptionMenuButtonEnabled | MFSideMenuOptionBackButtonEnabled | MFSideMenuOptionShadowEnabled;
    MFSideMenuPanMode panMode = MFSideMenuPanModeNavigationBar;
    
    MFSideMenu *sideMenu = [MFSideMenu menuWithNavigationController:navigationController sideMenuController:sideMenuController  location:MFSideMenuLocationLeft options:options panMode:panMode];
    
    sideMenuController.sideMenu = sideMenu;
    
    return sideMenu;
}

#pragma mark - UINavigationController & background image

- (UINavigationController *)customizedNavigationController:(UIViewController*)root{
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:root];
    [navController navigationBar];
    
    NSMutableData *data = [NSMutableData data];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:navController forKey:@"root"];
    [archiver finishEncoding];
    
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    [unarchiver setClass:[SCNavigationBar class] forClassName:@"UINavigationBar"];
    UINavigationController *customizedNavController = [unarchiver decodeObjectForKey:@"root"];
    [unarchiver finishDecoding];
    
    SCNavigationBar *navBar = (SCNavigationBar *)[customizedNavController navigationBar];
    [navBar setTintColor:[UIColor blackColor]];
    [navBar setBackgroundImage:[UIImage imageNamed:@"NavigationBarBcg.png"] forBarMetrics:UIBarMetricsDefault];
    
    return customizedNavController;
}

#pragma mark - PushNotificationManager

- (void) onPushAccepted:(PushNotificationManager *)manager {

}

- (void)application:(UIApplication *)app didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)devToken {
	[pushManager handlePushRegistration:devToken];
    
	NSString *token = [pushManager getPushToken];
	NSLog(@"Push token: %@", token);
}

- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err {
	NSLog(@"Error registering for push notifications. Error: %@", err);
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
	[pushManager handlePushReceived:userInfo];
}


#pragma mark - App Delegate

- (void)applicationWillResignActive:(UIApplication *)application{
}

- (void)applicationDidEnterBackground:(UIApplication *)application{
}

- (void)applicationWillEnterForeground:(UIApplication *)application{
}

- (void)applicationDidBecomeActive:(UIApplication *)application{
}

- (void)applicationWillTerminate:(UIApplication *)application{
}


@end
